package org.acghub.common.registry;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface MultiRegistry<K, V> extends Registry<K, List<V>> {

    @Override
    List<V> lookup(K k);

    @Override
    Map<K, List<V>> lookupAll();

    default V lookupOne(K k) {
        List<V> list = lookup(k);
        return list == null || list.isEmpty() ? null : list.get(0);
    }

    default Map<K, V> lookupAllOne() {
        Map<K, List<V>> map = lookupAll();
        if (map == null || map.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<K, V> mapOne = new HashMap<>();
        for (Map.Entry<K, List<V>> entry : map.entrySet()) {
            mapOne.put(entry.getKey(), entry.getValue().get(0));
        }
        return mapOne;
    }
}
